package com.citusdata.elven.datastore;

import java.util.Arrays;


public class DatastoreValue {
  private final byte[] bytes;

  public DatastoreValue(byte[] bytes) {
    // Note that we perform a shallow copy here with the expectation that this
    // class will get its byte array from BDB JE's DatabaseEntry. Since
    // DatabaseEntry internally allocates a new byte array, keeping a reference
    // to the byte array is always safe.
    this.bytes = bytes;
  }

  public boolean equals(Object obj) {
    if (!(obj instanceof DatastoreValue)) {
      return false;
    }

    DatastoreValue value = (DatastoreValue) obj;
    return (Arrays.equals(value.bytes, bytes));
  }

  public int hashCode() {
    int result = 17;
    for (int i = 0; i < bytes.length; ++i) {
      result = 37*result + ((int) bytes[i]);
    }
    return result;
  }

  public byte[] getBytes() {
    return bytes;
  }
}
